"""
销量预测接口
"""
# sfb
import json
from fastapi import APIRouter
from pydantic import BaseModel

# zjb

from core import getPredictResult, predictSale, predictSaleTradition
from core.utils import SelfJSONResponse, SelectJSONResponse, computeMD5

sales = APIRouter(tags=["销量预测"], prefix="/salesPredict")


class SaleData(BaseModel):
    yearAndMonth: list
    salesData: list
    series: str
    seriesId: str

    def getJson(self):
        return {
            'yearAndMonth': self.yearAndMonth,
            'salesData': self.salesData,
            'series': self.series,
            'seriesId': self.seriesId
        }


@sales.get("/getAllResult", summary="获取所有销量预测结果")
async def getAllSalesResult(seriesId=None, seriesName=None):
    return SelfJSONResponse(code=200,
                            data=getPredictResult(seriesId, seriesName),
                            msg="查询成功！")


@sales.post("/predictSaleOnline", summary="在线汽车销量预测")
async def predictSaleOnline(data: SaleData, ratio: float):
    result = predictSale(data.getJson(), ratio)
    return SelfJSONResponse(code=200,
                            data=result,
                            msg="训练成功！")


@sales.post("/predictSaleOnlineTradition", summary="在线汽车销量传统预测")
async def predictSaleOnlineTradition(data: SaleData, forcastMonth: int):
    result = predictSaleTradition(data.getJson(), forcastMonth)
    return SelfJSONResponse(code=200,
                            data=result,
                            msg="训练成功！")
